<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>开启摄像头</title>
  <style>
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    #video {
      width: 100%;
      height: 300px;
      border: 2px solid red;
    }
  </style>
</head>

<body>
  <video id="video" src=""></video>
  <input type="button" value="点击我吧">
</body>
<script>
  "use strict";
  const video = document.querySelector("#video");
  document.querySelector("input").addEventListener("click", () => {
    navigator.mediaDevices.getUserMedia({
      video: true,
      video: {
        width: 600,
        height: 600
      }
    }).then(res => {
      video.srcObject = res;
      video.play();
    }).catch(err => {
      console.error(err);
    });
  });
</script>

</html>